﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using XCharts;
using LitJson;
using Script.UI;
using MTUnity.Utils;
using System;
public class UI_StockInOrOut : UIUpdateBase
{
    public Text Txt_StockInOrOutBitTitle;
    public Text Txt_StockInOrOutSmallTitle1;
    public Text Txt_StockInOrOutSmallTitle2;
    public Text Txt_StockInOrOut;
    public Text Txt_StockInOrOutEfficient;
    public LineChart LineChart_StockInOrOutRecord;
    public Text Txt_RefreshTime;


    // Start is called before the first frame update
    void Start()
    {
        
    }

    #region Interface
    public override Dictionary<string, UpdateDel> RegisterProcessorMethod()
    {
        JsonData json = new JsonData();

        return new Dictionary<string, UpdateDel>
        {
            {JsonFildName.Warehouse.Current_StockInOrOut , UpdateCurrentStockInOrOut },
            {JsonFildName.Warehouse.Record_StockInOrOut , UpdateRecordStockInOrOut },
            {JsonFildName.Warehouse.StockInOrOutRefreshTime , UpdateRefreshTime },
        };
    }
	#endregion

	#region UpdateInterface
    public void UpdateCurrentStockInOrOut(JsonData jsonData)
    {
        UpdateStockInOrOut(JsonUtils.ToInt(jsonData[JsonFildName.Warehouse.InOrOutCount]));
        UpdateStockInOrOutEfficient(JsonUtils.ToDouble(jsonData[JsonFildName.Warehouse.InOrOutEfficient]));
    }

    public void UpdateRecordStockInOrOut(JsonData jsonData)
    {
        List<KeyValuePair<double, double>> temp = new List<KeyValuePair<double, double>>();
        for (int i = 0; i < jsonData.Count; i++)
        {
            temp.Add(
                new KeyValuePair<double, double>(
                    JsonUtils.ToDouble(jsonData[i][JsonFildName.Common.Efficient]), 
                    JsonUtils.ToDouble(jsonData[i][JsonFildName.Warehouse.Average])
                    )
                );
        }
        UpdateRecordStockInOrOut(temp);
    }

    public void UpdateRefreshTime(JsonData jsonData)
    {
        UpdateRefreshTime(JsonUtils.ToLong(jsonData));
    }
    #endregion

    private void UpdateTitle()
    {

    }

    private void UpdateStockInOrOut(int iStockInOrOut)
    {
        Txt_StockInOrOut.text = iStockInOrOut.ToString();
    }

    private void UpdateStockInOrOutEfficient(double dEfficient)
    {
        Txt_StockInOrOutEfficient.text = string.Format("{0}", ((int)dEfficient).ToString());
    }

    public void UpdateRecordStockInOrOut(List<KeyValuePair<double , double>> list)
    {
        for (int i = 0; i < list.Count; i++)
        {
            LineChart_StockInOrOutRecord.UpdateData(0, i, (float)list[i].Key);
            LineChart_StockInOrOutRecord.UpdateData(1, i, (float)list[i].Value);
        }
    }

    public void UpdateRefreshTime(long lRefreshTime)
    {
        DateTime datatime = TimeUtil.FromTimestamp(lRefreshTime);
        Txt_RefreshTime.text = string.Format("数据更新 {0}", datatime.ToString("{yyyy-MM-dd HH:mm:ss}"));
    }
}
